// pages/equipmentInfo/equipmentInfo.js
import Notify from '@vant/weapp/notify/notify';
import Dialog from '@vant/weapp/dialog/dialog';
import {dateFormat} from '../../utils/util';
import mockPoint from './mockPoint';
let includePoints = []
let points = mockPoint.map(({location}) => {
  let latlng = location.split(',');
  includePoints.push({
    latitude: latlng[1], longitude: latlng[0]
  })
  return  {latitude: latlng[1], longitude: latlng[0]}
});
Page({
  /**
   * 页面的初始数据
   */
  data: {
      tabs: ['基础信息', '状态信息', '状态明细', '轨迹记录', '操作记录'],
      refreshed: true, // 将本属性设置为true，收起下拉刷新，可多次设置为true（即便原来已经是true了）
      skeleton: true, // 骨架屏
      bottomLoad: false,
      disableBottomLoad: false,
      active: 0,
      currentDate: dateFormat('YYYY-mm-dd', new Date().getTime()),
      info: {
        prop: '测试数据',
        address: '测试设备定位地址。。。。。。。',
        Latitude:36.88296829686465,
        Longitude:118.7401055345469
      },
      // 基础信息
      infoList: [
        {label: '设备名称', prop: 'prop'},
        {label: '设备描述', prop: 'prop'},
        {label: '设备型号', prop: 'prop'},
        {label: '备注', prop: 'prop'},
        {label: '所属厂家', prop: 'prop'},
        {label: '所属组织', prop: 'prop'},
      ],
      time: null,
      status: {
        prop: '测试数据'
      },
      // 状态信息
      statusList: [
        {label: '经度', prop: 'JINGDU'},
        {label: '纬度', prop: 'WEIDU'},
        {label: '环境温度', prop: 'WENDU'},
        {label: '供电电压', prop: 'DIANYA'},
        {label: 'U相电流显示', prop: 'DB2.D76'},
        {label: '有功功率显示', prop: 'DB2.D80'},
        {label: 'WU线电压显示', prop: 'DB2.D84'},
        {label: 'VW线电压显示', prop: 'DB2.D88'},
        {label: 'UV线电压显示', prop: 'DB2.D92'},
        {label: '发电频率显示', prop: 'DB2.D96'},
        {label: '转速百分比显示', prop: 'DB2.D116'},
        {label: '温度最大值', prop: 'DB2.D120'},
        {label: '燃油箱液位显示', prop: 'DB2.D132'},
        {label: '机组状态显示', prop: 'DB2.D152'},
        {label: '运行指示', prop: 'Q24.0'},
        {label: '控制器工作', prop: 'Q24.1'},
        {label: '报警指示', prop: 'Q24.2'},
        {label: '紧急停机指示', prop: 'Q24.3'},
        {label: '正常停机指示', prop: 'Q24.4'},
        {label: '启动2', prop: 'Q26.3'},
        {label: '启动3', prop: 'Q26.4'},
        {label: '启动4', prop: 'Q26.5'},
        {label: '供油泵', prop: 'Q26.6'},
        {label: '左点火线圈', prop: 'Q27.0'},
        {label: '右点火线圈', prop: 'Q27.1'},
        {label: '启动阀', prop: 'Q27.2'},
        {label: 'GB断路器分闸状态', prop: 'Q27.3'},
        {label: '发电运行', prop: 'Q27.4'},
        {label: '启动运行', prop: 'Q27.5'},
      ],
      // 状态明细 
      statusDetailList: [
        {label: '冷转', prop: 'prop'},
        {label: '启动', prop: 'prop'},
        {label: '正常停机', prop: 'prop'},
        {label: '紧急停机', prop: 'prop'},
        {label: '复位', prop: 'prop'},
        {label: '加燃料', prop: 'prop'},
        {label: '减燃料', prop: 'prop'},
        {label: '百叶活门应急接通', prop: 'prop'},
        {label: 'GB断路器分闸状态', prop: 'prop'},
        {label: 'GB断路器合闸状态', prop: 'prop'},
        {label: 'GB断路器故障', prop: 'prop'},
        {label: '储能完成', prop: 'prop'},
        {label: '风机1工作', prop: 'prop'},
        {label: '风机2工作', prop: 'prop'},
        {label: '滑油箱液位低', prop: 'prop'},
        {label: '滑油滤堵塞', prop: 'prop'},
        {label: '金属屑信号', prop: 'prop'},
        {label: '燃油箱液位低', prop: 'prop'},
        {label: '双联粗油滤堵塞', prop: 'prop'},
        {label: '双联细油滤堵塞', prop: 'prop'},
        {label: '细油滤堵塞', prop: 'prop'},
        {label: '吸油滤堵塞', prop: 'prop'},
        {label: 'DI备用346', prop: 'prop'},
        {label: 'DI备用347', prop: 'prop'},
        {label: '开进排气门', prop: 'prop'},
        {label: '关进排气门', prop: 'prop'},
        {label: 'DI备用3', prop: 'prop'},
        {label: '排气门开到位', prop: 'prop'},
        {label: '排气门关到位', prop: 'prop'},
        {label: 'DI备用4', prop: 'prop'},
        {label: 'DI备用5', prop: 'prop'},
        {label: 'DI备用6', prop: 'prop'},
        {label: '百叶窗开到位', prop: 'prop'},
        {label: '百叶窗关到位', prop: 'prop'},
        {label: '百叶窗2开到位', prop: 'prop'},
        {label: '百叶窗2关到位', prop: 'prop'},
        {label: '运行指示', prop: 'prop'},
        {label: '控制器工作', prop: 'prop'},
        {label: '报警指示', prop: 'prop'},
        {label: '紧急停机指示', prop: 'prop'},
        {label: '正常停机指示', prop: 'prop'},
        {label: '防火阀', prop: 'prop'},
        {label: '截止阀', prop: 'prop'},
        {label: '风机1', prop: 'prop'},
        {label: '风机2', prop: 'prop'},
        {label: '启动励磁', prop: 'prop'},
        {label: '启动P3', prop: 'prop'},
        {label: '启动1', prop: 'prop'},
        {label: '启动2', prop: 'prop'},
        {label: '启动3', prop: 'prop'},
        {label: '启动4', prop: 'prop'},
        {label: '供油泵', prop: 'prop'},
        {label: '左点火线圈', prop: 'prop'},
        {label: '右点火线圈', prop: 'prop'},
        {label: '启动阀', prop: 'prop'},
        {label: 'GB断路器分闸', prop: 'prop'},
        {label: '发电运行', prop: 'prop'},
        {label: '启动运行', prop: 'prop'},
        {label: '储能', prop: 'prop'},
        {label: '输出电源', prop: 'prop'},
        {label: '开百叶窗1', prop: 'prop'},
        {label: '关百叶窗1', prop: 'prop'},
        {label: '开百叶窗2', prop: 'prop'},
        {label: '关百叶窗2', prop: 'prop'},
        {label: '开排气门', prop: 'prop'},
        {label: '关排气门', prop: 'prop'},
      ],
      trackShow: false,
      // 轨迹记录
      trackList: [
          {
            points,
            color: '#4e6ef2',
            width: 5,
            arrowLine: true
          }
      ],
      includePoints: [],
      // 操作记录
      list: [
        {title: '出库', subTitle: '测试出库操作'},
        {title: '入库', subTitle: '测试入库操作'},
        {title: '出库', subTitle: '测试出库操作'},
        {title: '入库', subTitle: '测试入库操作'},
        {title: '出库', subTitle: '测试出库操作'},
        {title: '入库', subTitle: '测试入库操作'},
        {title: '出库', subTitle: '测试出库操作'},
        {title: '入库', subTitle: '测试入库操作'},
        {title: '出库', subTitle: '测试出库操作'},
        {title: '入库', subTitle: '测试入库操作'},
        {title: '出库', subTitle: '测试出库操作'},
        {title: '入库', subTitle: '测试入库操作'},
        {title: '出库', subTitle: '测试出库操作'},
        {title: '入库', subTitle: '测试入库操作'},
        {title: '出库', subTitle: '测试出库操作'},
        {title: '入库', subTitle: '测试入库操作'},
        {title: '出库', subTitle: '测试出库操作'},
        {title: '入库', subTitle: '测试入库操作'},
      ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      wx.getLocation();
      this.setData({
        includePoints: includePoints
      })
      console.log(options);
      this.refresh();
  },
  location() {
    let {info} = this.data;
    wx.openLocation({
      latitude: info.Latitude,
      longitude: info.Longitude,
      name: info.address
    })
  },
  // 获取状态数据
  getStatusData() {
    console.log('ss');
    let that = this;
    wx.request({
      url: 'http://49.234.239.67:28080/test/getjson/data?gateWayId=89860439102080117445',
      success({data}) {
        let resData = JSON.parse(data[0]['josnData']);
        that.setData({
          status: resData
        })
      }
    })
  },
  // 清楚定时器
  clrarStatusTime() {
        if(this.data.time) {
          clearInterval(this.data.time);
        }
  },
  // 编辑
  edit() {},
  // 删除
  del() {
    Dialog.confirm({
      title: '确认删除？',
      message: '删除过后设备将不存在',
    })
      .then(() => {
        wx.showLoading({
          title: '请求中',
        });
        setTimeout(() => {
          wx.hideLoading();
        // 成功通知
        Notify({ type: 'success', message: '删除成功' });
        // 警告通知
        // Notify({ type: 'warning', message: '通知内容' });
          setTimeout(() => {
            wx.navigateBack();
          }, 500)
        }, 1000)
      })
      .catch(() => {
      });
  },
  // 出库
  delivery() {
    wx.showLoading({
      title: '请求中',
    });
    setTimeout(() => {
      wx.hideLoading();
    // 成功通知
    Notify({ type: 'success', message: '出库成功' });
    // 警告通知
    // Notify({ type: 'warning', message: '通知内容' });
      this.refresh();
    }, 1000)
  },
  // 入库
  put() {
    wx.showLoading({
      title: '请求中',
    });
    setTimeout(() => {
      wx.hideLoading();
      // 成功通知
      Notify({ type: 'success', message: '入库成功' });
      // 警告通知
      // Notify({ type: 'warning', message: '通知内容' });
      this.refresh();
    }, 1000)
  },
  refresh() {
    this.setData({
      skeleton: true
    });
    this.getData();
  },
  // 请求数据接口
  getData() {
   
    if(this.data.active == 1) {
      console.log('ss');
      let time = setInterval(() => {
        this.getStatusData();
      }, 1000);
      this.setData({
        time
      })
    } else{
      this.clrarStatusTime();
    }
    setTimeout(() => {
      this.setData({
        disableBottomLoad: true,
        bottomLoad: false,
        skeleton: false,
        refreshed: true,
      });
    }, 1000)
  },
  onTabClick(event) {
    this.setData({
      active: event.detail.name,
      skeleton: event.detail.name != 3
    }, () => {
      if(event.detail.name != 3) {
        wx.nextTick(() => {
          this.getData();
        })
      }
    });
  },
  showTrackDate() {
    this.setData({
      trackShow: !this.data.trackShow
    })
  },
  onDateInput({detail}) {
    this.setData({
      currentDate:  dateFormat('YYYY-mm-dd', detail)
    });
    this.showTrackDate();
  },
  onUnload() {
    this.clrarStatusTime();
  },
})